Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
jest.fn().mockReturnValue("hello") returns undefined when called in a beforeAll() but not in a beforeEach()

How come when I create a jest.fn() function and then call mockReturnValue("hello") on it inside of a beforeAll() I get undefined when I try to console.log the value of it in a test but not when I call the mockReturnValue("hello") inside of a beforeEach()?

import App from "./App"

describe("App", () => {

  const mock = jest.fn()

  beforeAll(() => {
    mock.mockReturnValue("hello")
  })

  beforeEach(() => {
  })

  afterEach(() => {
  })

  afterAll(() => {
  })

  it("should console.log the value 'hello'", () => {
    console.log(mock()) // undefined
  })
})

beforeAll print undefined

however, when I call the mockReturnValue("hello") inside a beforeEach, it prints hello.

import App from "./App"

describe("App", () => {

  const mock = jest.fn()

  beforeAll(() => {
  })

  beforeEach(() => {
   mock.mockReturnValue("hello")
  })

  afterEach(() => {
  })

  afterAll(() => {
  })

  it("should console.log the value 'hello'", () => {
    console.log(mock()) // hello
  })
})

beforeAll print hello

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Thanks to @jonrsharpe, I was able to track my issue down.

I am running my test via node_modules/react-scripts instead of via node_modules/jest

by default, according to this other StackOverflow question and answer

by default in react-scripts node_modules/react-scripts/scripts/utils/createJestConfig.js the jest configuration is set with "resetMocks": true

So what I did, inside of my package.json file was put the jest configuration of

  "jest": {
    "resetMocks": false
  }

And re-ran my test, and it passed as expected

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!